<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>编辑商品</title>
		<link rel="stylesheet" href="__AGENT__/css/css.css" />
		<script type="text/javascript" src="__PUBLIC__/static/js/jquery.min.js"></script>
		<link rel="stylesheet" href="__PUBLIC__/static/layui/css/layui.css">
		<script src="__PUBLIC__/static/layui/layui.js"></script>
	</head>

	<body>
		<form id="my_form" class="layui-form update_form" action="{:url('shop/goods_edit')}" method="post">
			<div class="layui-form-item">
				<label class="layui-form-label">商品编码</label>
				<div class="layui-input-block update_shurk">
					<input type="text" name="goods_no" lay-verify="goods_no" value="{$goods_info.goods_no}" placeholder="商品编码" autocomplete="off" class="layui-input">
					<input type="hidden" name="id" value="{$goods_info.goods_id}">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">商品名称</label>
				<div class="layui-input-block update_shurk">
					<input type="text" name="goods_name" lay-verify="required" value="{$goods_info.goods_name}" placeholder="商品名称" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item update_scbtn">
				<label class="layui-form-label">上传</label>
				<div class="layui-upload">
				  <button type="button" class="layui-btn" id="test1">上传图片</button>
				  <div class="layui-upload-list" style="    margin-left: 10%;">
				    <img class="layui-upload-img" id="demo1" style="width: 150px;height: 150px;" src="{$goods_info.thumb}">
				    <p id="demoText"></p>
				  </div>
				</div>   
				<input type="hidden" name="thumb" id='thumb' value="{$goods_info.thumb}" />
			</div> 
			<div class="layui-form-item">
				<label class="layui-form-label">平台类别</label>
				<div class="layui-input-block" style="float: left; margin-right: 20px; margin-left: 0;">
					<select name="cate_id" id='cate_id' lay-verify="required" lay-filter="sel_1">
						{volist name="platform_1" id="vo"}
							{if $vo.id == $goods_info.cate_id}
								<option value="{$vo.id}">{$vo.type_name}</option>
							{/if}
						{/volist}
						{volist name="platform_1" id="vo"}
							{if $vo.id != $goods_info.cate_id}
								<option value="{$vo.id}">{$vo.type_name}</option>
							{/if}
						{/volist}
					</select>
				</div>
				<div class="layui-input-block" style="float: left; margin-left: 0;">
					<select name="cate2_id" id='cate2_id' lay-verify="required" lay-filter="sel_2">
						{volist name="platform_2" id="vo"}
							 {volist name="vo" id="item" key="k"}
							 		{if $item.id == $goods_info.cate2_id}
							 			<option value="{$item.id}">{$item.type_name}</option>
							 		{/if}
						    {/volist}
						{/volist}
						{volist name="platform_2" id="vo"}
							 {volist name="vo" id="item" key="k"}
							 		{if $item.id != $goods_info.cate2_id}
							 			<option value="{$item.id}">{$item.type_name}</option>
							 		{/if}
						    {/volist}
						{/volist}
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">商品分类</label>
				<div class="layui-input-block" style="float: left; margin-right: 20px; margin-left: 0;">
					<select name="trade_id" lay-verify="required">
						{volist name="agent_type" id="vo"}
							{if $vo.id == $goods_info.trade_id}
								<option value="{$vo.id}">{$vo.type_name}</option>
							{/if}
						{/volist}
						{volist name="agent_type" id="vo"}
							{if $vo.id != $goods_info.trade_id}
								<option value="{$vo.id}">{$vo.type_name}</option>
							{/if}
						{/volist}
					</select>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">归属门店</label>
				<div class="layui-input-block" style="float: left; margin-right: 20px; margin-left: 0;">
					<select name="chainstore_id" lay-verify="required">
						{volist name="storelist" id="vo"}
							{if $vo.id == $goods_info.chainstore_id}
								<option value="{$vo.id}">{$vo.store_name}</option>
							{/if}
						{/volist}
						<option value="0">通用</option>
						{volist name="storelist" id="vo"}						
							{if $vo.id != $goods_info.chainstore_id}
								<option value="{$vo.id}">{$vo.store_name}</option>
							{/if}
						{/volist}
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">成本价</label>
				<div class="layui-input-block update_shurk"  style="width: 80px; margin-right: 10px;">
					<input type="text" name="cost_price" id="cost_price" value="{$goods_info.cost_price}"  placeholder="成本价"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
				</div> <div class="layui-form-mid layui-word-aux">元</div>
			</div>			
			<div class="layui-form-item">
				<label class="layui-form-label">零售价</label>
				<div class="layui-input-block update_shurk"  style="width: 80px; margin-right: 10px;">
					<input type="text" name="sale_price" id="sale_price" value="{$goods_info.sale_price}"  placeholder="销售价"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
				</div> <div class="layui-form-mid layui-word-aux">元，送</div>
				<div class="layui-input-block update_shurk" style="width: 80px; margin-left: 10px; margin-right: 10px;">
					<input type="text" name="zs_price" value="{$goods_info.zs_price}" required lay-verify="required" placeholder="赠送" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">鼓励金，*请输入数字</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">会员价</label>
				<div class="layui-input-block update_shurk hui"  style="width: 80px; margin-right: 10px;">
					<input type="text" name="aprice" id="aprice" value="<?php echo $goods_info['sale_price'] - $goods_info['dk_price'] ?>"  placeholder="会员价"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
				</div><div class="layui-form-mid layui-word-aux">元, &nbsp;+ </div>
				<div class="layui-input-block update_shurk" style="width: 80px; margin-left: 10px; margin-right: 10px;">
					<input readonly="readonly" type="text" name="dk_price" id="dk_price" value="{$goods_info.dk_price}" required lay-verify="required" placeholder="抵扣" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">鼓励金，*请输入数字</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">库存</label>
				<div class="layui-input-block update_shurk"  style="width: 80px; margin-right: 10px;">
					<input type="text" name="stock"  placeholder="库存" value="{$goods_info.stock}" lay-verify="required"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
				</div>
				<div class="layui-form-mid layui-word-aux">*请输入数字</div>
				<label class="layui-form-label">计量单位</label>
				<div class="layui-input-block" style="float: left; margin-right: 20px; margin-left: 0;">
					<select name="unit">
						<option value=""></option>
						{volist name="$Think.config.units" id="vo"}
							<option {if $goods_info.unit==$vo}selected="selected" {/if} value="{$vo}">{$vo}</option>
						{/volist}
					</select>
				</div>
			</div>
			
			<!--<div class="layui-form-item">
				<label class="layui-form-label">快递类型</label>
				<div class="layui-input-block">
					<input type="radio" name="express_type" value="1" title="快递订单" {if $goods_info.express_type == 1}checked{/if}>
					<input type="radio" name="express_type" value="2" title="O2O订单" {if $goods_info.express_type == 2}checked{/if}>
					<input type="radio" name="express_type" value="3" title="可快递，可自取" {if $goods_info.express_type == 3}checked{/if}>
				</div>
			</div>-->
			<div class="layui-form-item">
				<label class="layui-form-label">是否推荐</label>
				<div class="layui-input-block">
					<input type="radio" name="is_recommend" value="2" title="是" {if $goods_info.is_recommend == 2}checked{/if}>
					<input type="radio" name="is_recommend" value="1" title="否" {if $goods_info.is_recommend == 1}checked{/if}>
				</div>
			</div>			
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">商品详情</label>
			</div>
			<div class="layui-form-item layui-form-text" style="width: 95%;margin-left: 3%;">
				<div id="div1" class="toolbar" style="background-color: #d4cece;"></div>
	    		<div id="div2" class="text" style="height: 350px;background-color: #f7f7f7;" ></div>
	    		<textarea name="content" required lay-verify="required" id="content" style="display: none;">{$goods_info["content"]}</textarea>
			</div>
			<div class="layui-form-item update_btnbox" style="text-align: center;margin-left:unset">
				<div class="layui-input-block" style="    max-width: unset;float: unset;">
					<button onclick="content_vale()" id='change_but' type="button" lay-submit class="layui-btn" lay-filter="demo1">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</body>
</html>
<script type="text/javascript" src="__PUBLIC__/static/release/wangEditor.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/js/assist.js"></script>
<script>
	$(".hui input[type='text']").change(function(){
		$("#dk_price").val(parseFloat(($("#sale_price")[0].value)-parseFloat($("#aprice")[0].value)).toFixed(2));
	})
	//二级分类
	var platform_2 = <?php echo json_encode($platform_2);?>;
	
	//富文本编辑器
    var E = window.wangEditor;
    var editor1 = new E('#div1', '#div2');
    editor1.customConfig.uploadImgShowBase64 = true
    editor1.create();
    
    $('#demo1').attr('src', "{$goods_info['thumb']}"); //图片显示
    
    
    editor1.txt.html($('#content').val());
    $('#thumb').val("{$goods_info['thumb']}");
    
    
    function content_vale(){
    	$("#content").val(editor1.txt.html());
    }
    //提交表单
	function set_content(){
  		 $.ajax({
             	type:"post",
                url:"{:url('shop/goods_edit')}",
                data: $('#my_form').serialize(),//表单数据
                success:function(data){
                    if(data.code==200){
                    	layer.msg('修改成功', {
						  icon: 1,
						  time: 1500 //2秒关闭（如果不配置，默认是3秒）
						}, function(){
							//window.parent.location.reload();
							var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
					       	parent.layer.close(index); 
						});
                    }else{
                    	layer.msg('添加失败！');//保存成功提示
                    }
                },error:function(data){
                	var a = data;
                }
        });
  		
  	}
	
	
	//图片上传
	layui.use(['form','upload'], function(){
  		var $ = layui.jquery,upload = layui.upload,form = layui.form;
  		
  		
  		//普通图片上传
		var uploadInst = upload.render({
			elem: '#test1'
		    ,auto: false //选择文件后不自动上传
		    ,choose: function(obj){
			    //将每次选择的文件追加到文件队列
			    var files = obj.pushFile();
			    
			    //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
			    obj.preview(function(index, file, result){
			    	$('#demo1').attr('src', result); //图片链接（base64）
			    	var sendData = result.replace("data:"+'image/jpeg'+";base64,",'');
			    	$.post("{:url('agent/shop/upload')}",{value:sendData},function(data){ 
	                            if(data.code == '200'){  
	                            	$('#thumb').val(data.url);
	                            }else{  
	                                layer.msg(data.msg);
	                            }  
                    		});
			    });
			}
		    ,url: "{:url('agent/shop/upload')}"
		    ,accept:'images'
		});
	  
		//监听下拉框
		form.on('select(sel_1)', function(data){
			
			var cate_id = data.value;
			
			var cate2_type = platform_2[cate_id];
			var select_htm = '';
			
			if(cate2_type && cate2_type.length<1){
				select_htm = "<option value=''></option>";
			}else{
				for(var i=0;i<cate2_type.length;i++){
					select_htm += "<option value='"+cate2_type[i].id+"'>"+cate2_type[i].type_name+"</option>";
				}
			}
			
			$('#cate2_id').empty();
			$('#cate2_id').append(select_htm);
			
			form.render('select');
			
			
		});  
		
		//监听提交
		form.on('submit(demo1)', function(data){
			$('#change_but').addClass("layui-btn-disabled");
			set_content();
			
		    return false;
		});
	  
	  
	})
</script>
